package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.*;
import com.spark.incoicings.service.CustomerService;
import com.spark.incoicings.service.SalesReturnService;
import com.spark.incoicings.vo.CustomerVO;
import com.spark.incoicings.vo.SalesReturnVO;
import com.spark.invoicings.entity.OrderDetail;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.entity.SalesReturn;
import com.spark.invoicings.entity.WarehouseReceipt;
import com.spark.invoicings.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SalesReturnServiceImpl extends ServiceImpl<SalesReturnMapper, SalesReturn> implements SalesReturnService{

    @Resource
    private SalesReturnMapper salesReturnMapper;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private WarehouseReceiptMapper warehouseReceiptMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private ProductMapper productMapper;
    @Override
    public Result selectSalesReturnCount() {
        List<SalesReturnVO> salesReturns = salesReturnMapper.selectSalesReturnCount();
        salesReturns.forEach(s->{
            String year = s.getMonth().substring(0,4);
            String month = s.getMonth().substring(4);
            String yearMonth = year+"年"+month+"月";
            s.setMonth(yearMonth);
        });
        return Result.success(200,"success",salesReturns);
    }
    @Override
    public List<SalesReturn> selectSalesReturnAll(Map<String, Object> map) {
        List<SalesReturn> salesReturns = salesReturnMapper.selectSalesReturnAll(map);
        salesReturns.forEach(salesReturn -> {
            WarehouseReceipt warehouseReceipt = warehouseReceiptMapper.selectOne(new QueryWrapper<WarehouseReceipt>()
                    .eq("return_id", salesReturn.getReturnId()));
            salesReturn.setWarehouseReceipt(warehouseReceipt);
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>()
                    .eq("sales_order_id",salesReturn.getSalesOrder().getOrderId()));
            salesReturn.getSalesOrder().setOrderDetail(orderDetails);
            orderDetails.forEach(orderDetail -> {
                Product product = productMapper.selectById(orderDetail.getProductId());
                orderDetail.setProducts(product);
            });
            CustomerVO customerVO = customerMapper.selectCustomerVo(salesReturn.getCustomer().getCustomerId());
            salesReturn.getCustomer().setCustomerRating(customerVO.getCustomerRating());
            salesReturn.getCustomer().setCustomerSource(customerVO.getCustomerSource());
            salesReturn.getCustomer().setBillingMethod(customerVO.getBillingMethod());
        });
        return salesReturns;
    }
}
